import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:seedfour/app/models/invite_user_entity.dart';

import '../../../routes/app_routes.dart';
import '../../../services/api_service.dart';

class MineInviteLogic extends GetxController {
  final apiService = Get.find<ApiService>();
  final loadingTip = '加载中...';
  final noMoreTip = '没有更多了';

  var isLoading = true.obs;
  var bottomTips = ''.obs;
  var inviteUsers = <InviteUserEntity>[].obs;

  final scrollController = ScrollController();

  @override
  void onInit() {
    super.onInit();
    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        _getInviteInfo();
      }
    });

    _getInviteInfo();
  }

  void toInviteSettles() {
    Get.toNamed(RouteGet.mineInviteSettles);
  }

  @override
  void onClose() {
    scrollController.dispose();
    super.onClose();
  }

  void _getInviteInfo() {
    inviteUsers.clear();
    apiService.getInviteUserList().then((invite) => {
          isLoading.value = false,
          if (invite != null)
            {
              inviteUsers.addAll(invite.data ?? []),
              bottomTips.value = noMoreTip
            }
          else
            {bottomTips.value = noMoreTip}
        });
  }
}
